package com.game.state;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;

import com.game.animation.AnimationGradualchange;
import com.game.animation.AnimationGradualchangeCallBack;
import com.game.animation.AnimationGradualchangeSquare;
import com.game.utils.ImageTool;

/**
 * 默认的logo状态
 * @author Administrator
 *
 */
public class BasicLogoState extends BasicState {
	//保存logo图片,logo图片命名方式:logo0.png;logo1.png
	private Bitmap[] logoImg;
	//logo图片总数
	private int logoCount=0;
	//当前显示第几张logo
	private int logoStep=-1;
	//logo切换的时间
	private int logoTime=2000;
	private BasicState firstState;
	
	public BasicLogoState(int logoCount,BasicState firstState){
		this.logoCount=logoCount;
		//若logo后没有state可跳转,则新建一个
		this.firstState=firstState;
		if(this.firstState==null){
			this.firstState=new BasicTitleState();
		}
		
	}
	
	@Override
	public void init(){
		logoImg=new Bitmap[logoCount];
		for(int i =0;i<logoCount;i++){
			logoImg[i]=ImageTool.getBitMap("logo"+i+".png");
		}
	}
	
	
	/**
	 * 启动logo切换线程*/
	public void startLogo(){
		new Thread(){
			@Override
			public void run() {
				long startTime= System.currentTimeMillis();
				//-1  -> 0
				logoStep++;
				while(logoStep<logoCount){
					if(!gameView.pause){
						Log.i("infos", "显示第"+logoStep+"张logo");
						DelayTime(logoTime);
						logoStep++;
					}
				}
				logoStep--;
				//渐变消失
				AnimationGradualchangeSquare gc=new AnimationGradualchangeSquare(BasicLogoState.this,new AnimationGradualchangeCallBack() {
					@Override
					public void CallBack(AnimationGradualchange animationGradualchange) {
						if(firstState!=null){
							gameView.changeState(firstState);
						}
					}
				},5,0,1000);
				gc.setHide(true);
				gc.setSpeedType(1);
				
				long endTime=System.currentTimeMillis();
				Log.i("infos", "logo 用时:"+(endTime-startTime));
				//logo显示完毕
//				gameView.drawing=false;
//				if(firstState!=null){
//					gameView.changeState(firstState);
//				}
				
			}
		}.start();
	}
	
	/**
	 * 逻辑处理,可以是一个循环线程*/
	@Override
	public void logic() {
		//logoStep初始值为-1,开始显示logo
		if(logoStep==-1){
			startLogo();
		}
	}

	@Override
	public void destory() {
		logoImg=null;
		System.gc();
	}

	@Override
	public void pointerPress(float x, float y) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pointerRelaese(float x, float y) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pointerMove(float x, float y) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void paintState(Canvas canvas) {
		if(logoStep<logoCount){
			if(logoStep!=-1){
				canvas.drawColor(Color.WHITE);
				ImageTool.drawImgCenter(canvas, logoImg[logoStep],null);
			}
		}
	}

	@Override
	public void logicInPaint() {
		// TODO Auto-generated method stub
		/**
		* @version 创建时间：2012-11-29 上午9:35:34
		* 
		*/
	}
}
